﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKArch.WallModule.EditWall
{
	public class CompoundStructureLayerAnalyzer
	{
		public static CompoundStructureLayerAnalyzer.OffsetForWallLocationLine AnalyzerOffset(Wall wall)
		{
			double num = wall.WallType.Width * 0.5;
			CompoundStructureLayerAnalyzer.OffsetForWallLocationLine offsetForWallLocationLine = default(CompoundStructureLayerAnalyzer.OffsetForWallLocationLine);
			offsetForWallLocationLine.m_WallCenterline = 0.0;
			offsetForWallLocationLine.m_FinishFaceExterior = num;
			offsetForWallLocationLine.m_FinishFaceInterior = -num;
			bool flag = false;
			double num2 = 0.0;
			IEnumerable<CompoundStructureLayer> layers = wall.WallType.GetCompoundStructure().GetLayers();
			List<CompoundStructureLayer> list = new List<CompoundStructureLayer>();
			foreach (CompoundStructureLayer compoundStructureLayer in layers)
			{
				if (compoundStructureLayer.MaterialId != ElementId.InvalidElementId)
				{
					list.Add(compoundStructureLayer);
				}
			}
			foreach (CompoundStructureLayer compoundStructureLayer2 in list)
			{
				MaterialFunctionAssignment function = compoundStructureLayer2.Function;
				if (!flag)
				{
                    if ((int)function == 1)
					{
						offsetForWallLocationLine.m_CoreExterior = num - num2;
						flag = true;
					}
				}
                else if ((int)function != 1)
				{
					offsetForWallLocationLine.m_CoreInterior = num - num2;
					break;
				}
				num2 += compoundStructureLayer2.Width;
			}
			offsetForWallLocationLine.m_CoreCenterline = (offsetForWallLocationLine.m_CoreExterior + offsetForWallLocationLine.m_CoreInterior) * 0.5;
			return offsetForWallLocationLine;
		}

		public static double GetOffsetForLocationLine(WallLocationLine locationLine, CompoundStructureLayerAnalyzer.OffsetForWallLocationLine offsetStruct)
		{
			double result = 0.0;
            switch ((int)locationLine)
			{
			case 0:
				result = offsetStruct.m_WallCenterline;
				break;
			case 1:
				result = offsetStruct.m_CoreCenterline;
				break;
			case 2:
				result = offsetStruct.m_FinishFaceExterior;
				break;
			case 3:
				result = offsetStruct.m_FinishFaceInterior;
				break;
			case 4:
				result = offsetStruct.m_CoreExterior;
				break;
			case 5:
				result = offsetStruct.m_CoreInterior;
				break;
			}
			return result;
		}

		public struct OffsetForWallLocationLine
		{
			public double m_WallCenterline;

			public double m_CoreCenterline;

			public double m_FinishFaceExterior;

			public double m_FinishFaceInterior;

			public double m_CoreExterior;

			public double m_CoreInterior;
		}
	}
}
